<template>
  <!-- 供货商管理 -->
  <div class="p-2">
    <!-- 顶部搜索 -->
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter"
      :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div class="mb-[10px]" v-show="showSearch">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true" label-width="68px">
            <el-form-item label="关键字" prop="search">
              <el-input v-model.trim="queryParams.search" placeholder="请输入关键字" clearable style="width: 200px"
                @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="handleQuery" icon="Search">搜索</el-button>
              <el-button @click="resetQuery" icon="Refresh">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>
    <el-card shadow="hover">
      <!-- 功能按钮 -->
      <template #header>
        <el-row :gutter="10">
          <el-col :span="1.5">
            <el-button type="primary" icon="Plus" plain @click="handleAdd()"
              v-hasPermi="['system:supplier:add']">新增</el-button>
          </el-col>
          <!-- <el-col :span="1.5">
            <el-button type="success" :disabled="single" icon="Edit" plain @click="handleUpdate()"
              v-hasPermi="['system:supplier:edit']">修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain :disabled="single" icon="Delete" @click="handleDelete()"
              v-hasPermi="['system:supplier:remove']">删除</el-button>
          </el-col> -->
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>
      <!-- 列表 -->
      <el-table ref="supTableRef" v-loading="loading" :data="supList" max-height="550"
        @selection-change="handleSelectionChange">
        <!-- <el-table-column type="selection" width="55" align="center" /> -->
        <el-table-column label="APPID" prop="merchantCode" :show-overflow-tooltip="true" align="center" width="" />
        <el-table-column label="APP名称" prop="name" :show-overflow-tooltip="true" align="center" width="" />
<!--        <el-table-column label="银行名称" prop="bankName" :show-overflow-tooltip="true" align="center" width="" />-->
<!--        <el-table-column label="银行编码" prop="bankCode" :show-overflow-tooltip="true" align="center" width="" />-->
<!--        <el-table-column label="银行账号" prop="bankAccount" :show-overflow-tooltip="true" align="center" width="" />-->
        <el-table-column label="状态" prop="enable" align="center" width="">
          <template #default="scope">
            <el-switch v-model="scope.row.enable" :active-value="1" :inactive-value="0"
              @change="handleStatusChange(scope.row)"></el-switch>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="创建时间" prop="createTime" align="center" :show-overflow-tooltip="true" width="140">
          <template #default="scope">{{ scope.row.createTime.slice(0, -3) }}</template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" align="center" width="180">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
                v-hasPermi="['system:supplier:edit']"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                v-hasPermi="['system:supplier:remove']"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-if="total > 0" v-model:total="total" v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>

    <el-dialog :title="dialog.title" v-model="dialog.visible" width="500px" append-to-body>
      <el-form ref="supFormRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="AppID" prop="merchantCode">
          <el-input v-model="form.merchantCode" clearable placeholder="请输入AppId" />
        </el-form-item>
        <el-form-item label="App名称" prop="name">
          <el-input v-model="form.name" clearable placeholder="请输入App名称" />
        </el-form-item>
<!--        <el-form-item label="银行名称" prop="bankName">-->
<!--          <el-select v-model="form.bankName" clearable placeholder="请选择银行名称" style="width: 100%">-->
<!--            <el-option v-for="item in bankNameList" :key="item.value" :label="item.label" :value="item.label"></el-option>-->
<!--          </el-select>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="银行编码" prop="bankCode">-->
<!--          <el-input v-if="bankCodeType" v-model="form.bankCode" clearable placeholder="请输入银行编码" />-->
<!--          <template v-else><span style="padding-left: 15px;">{{ form.bankCode }}</span></template>-->
<!--        </el-form-item>-->

<!--        <el-form-item label="银行账号" prop="bankAccount">-->
<!--          <el-input v-model="form.bankAccount" clearable placeholder="请输入银行账号" />-->
<!--        </el-form-item>-->
        <!-- <el-form-item label="状态" prop="enable">
          <el-radio-group v-model="form.enable">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item> -->
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts" name="Supplier">
import { listSupplier, addSupplier, updateSupplier, delSupplier, enableSupplier } from '@/api/system/supplier/index';
import { SupVO, SupForm, SupQuery } from '@/api/system/supplier/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const supList = ref<SupVO[]>([]);
const loading = ref(true)
const showSearch = ref(true)
const ids = ref<Array<string | number>>([])
const infos = ref<SupVO[]>([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const bankCodeType = ref(true)

const bankNameList: any = [
  { value: "01000000", label: "邮储银行" },
  { value: "01020000", label: "工商银行" },
  { value: "01030000", label: "农业银行" },
  { value: "01040000", label: "中国银行" },
  { value: "01050000", label: "建设银行" },
  { value: "03010000", label: "交通银行" },
  { value: "03020000", label: "中信银行" },
  { value: "03030000", label: "光大银行" },
  { value: "03040000", label: "华夏银行" },
  { value: "03050000", label: "民生银行" },
  { value: "03060000", label: "广发银行" },
  { value: "03080000", label: "招商银行" },
  { value: "03090000", label: "兴业银行" },
  { value: "03100000", label: "浦发银行" },
  { value: "04012900", label: "上海银行" },
  { value: "04031000", label: "北京银行" },
  { value: "04105840", label: "平安银行" },
]

const queryFormRef = ref<ElFormInstance>();
const supFormRef = ref<ElFormInstance>();

const initForm: SupForm = {
  name: '',
  bankName: '',
  bankCode: '',
  merchantCode: '',
  bankAccount: '',
  enable: 0,
}
const data = reactive<PageData<SupForm, SupQuery>>({
  form: { ...initForm },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    search: '',
  },
  rules: {
    merchantCode: [{ required: true, message: "AppId不能为空", trigger: "blur" }],
    name: [{ required: true, message: "APP名称不能为空", trigger: "blur" }],
  }
})
const { form, queryParams, rules } = toRefs(data)

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

/**
 * 查询供货商列表
 */
const getList = async () => {
  loading.value = true
  const res = await listSupplier(queryParams.value);
  supList.value = res.data.list;
  total.value = res.data.total;
  loading.value = false;
  // console.log(res);
  // console.log(supList.value);
}

/**
 * 搜索按钮操作
 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/**
 * 重置搜索
 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/**
 * 多选框选中数据
 */
const handleSelectionChange = (selection: SupVO[]) => {
  ids.value = selection.map((item: SupVO) => item.id);
  infos.value = selection.map((item: SupVO) => item);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/**
 * 添加供货商
 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = "添加供货商";
}

/**
 * 修改供货商
 */
const handleUpdate = (row?: SupVO) => {
  reset();
  if (row) {
    Object.assign(form.value, row); 1
  } else {
    Object.assign(form.value, infos.value[0]);
  }
  dialog.visible = true;
  dialog.title = "修改供货商";
}

/**
 * 删除供货商
 */
const handleDelete = async (row?: SupVO) => {
  try {
    const sup = row || infos.value[0];
    if (sup.enable == 1) {
      proxy?.$modal.msgError('请先执行禁用操作');
      return
    }
    await proxy?.$modal.confirm('是否确认删除供货商');
    await delSupplier(sup.id);
    getList();
    proxy?.$modal.msgSuccess('删除成功');
  } catch (error) { }
}

/**
 * 启用禁用
 */
const handleStatusChange = async (row: SupVO) => {
  try {
    let text = row.enable === 1 ? "启用" : "停用";
    await enableSupplier(row.id);
    proxy?.$modal.msgSuccess(text + "成功");
  } catch (error) {
    row.enable = row.enable === 1 ? 0 : 1;
  }
}

/**
 * 重置新增表单
 */
const reset = () => {
  form.value = { ...initForm };
  supFormRef.value?.resetFields();
}

/**
 * 提交按钮
 */
const submitForm = async () => {
  try {
    await supFormRef.value?.validate()
    form.value.id ? await updateSupplier(form.value) : await addSupplier(form.value);
    proxy?.$modal.msgSuccess(`${form.value.id ? '修改' : '添加'}成功`)
    dialog.visible = false
    getList()
  } catch (error) {
    // proxy?.$modal.msgError("操作失败")
  }
}

/**
 * 取消按钮
 */
const cancel = () => {
  reset()
  dialog.visible = false;
}

/**
 * 监听银行名称
 */
watch(
  () => form.value.bankName,
  (val, old) => {
    if (val) {
      const res: any = bankNameList.find((item: { label: any; }) => {
        return item.label === val;
      });
      bankCodeType.value = false;
      form.value.bankCode = res.value;
    } else {
      bankCodeType.value = true;
      form.value.bankCode = '';
    }
  },
)

onMounted(() => {
  getList();
});
</script>

<style scoped></style>
